package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserHouseBarDispatch;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserHouseBarDispatchDaoImpl extends TempDataObjectDao<UserHouseBarDispatch> {

    public UserHouseBarDispatch getUserHouseBarDispatch(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_house_bar_dispatch where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserHouseBarDispatch userHouseBarDispatch = parse(rs);
                return userHouseBarDispatch;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserHouseBarDispatch data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_house_bar_dispatch(userId,dispatchPatronsList) values (?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setString(i++, StringUtils.listToString(data.getDispatchPatronsList(), ","));
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserHouseBarDispatch data) {
        PreparedStatement call = null;
        String sql = "update t_u_house_bar_dispatch set dispatchPatronsList=? where userId=? ";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, StringUtils.listToString(data.getDispatchPatronsList(), ","));
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserHouseBarDispatch parseData(ResultSet rs) throws SQLException {
        UserHouseBarDispatch userHouseBarDispatch = new UserHouseBarDispatch();
        userHouseBarDispatch.setUserId(rs.getLong("userId"));
        userHouseBarDispatch.setDispatchPatronsList(StringUtils.stringToIntegerList(rs.getString("dispatchPatronsList"), ","));
        return userHouseBarDispatch;
    }
}
